home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / HSIM Code / Harmonic Sim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-01  |  16.7 KB  |  810 lines  |  [TEXT/KAHL]

  1. #include <strings.h>
  2. #include <storage.h>
  3.  
  4. #include <QuickDraw.h>
  5. #include <MacTypes.h>
  6. #include <FontMgr.h>
  7. #include <WindowMgr.h>
  8. #include <MenuMgr.h>
  9. #include <TextEdit.h>
  10. #include <DialogMgr.h>
  11. #include <EventMgr.h>
  12. #include <DeskMgr.h>
  13. #include <StdfilePkg.h>
  14. #include <FileMgr.h>
  15. #include <ToolboxUtil.h>
  16. #include <ControlMgr.h>
  17.  
  18. #include "Harmonic.h"
  19.  
  20.  
  21. /*************    globals    ***************/
  22.  
  23. int errno;                        /* required by the math lib */
  24.  
  25. /**************    static globals    *****************/
  26.  
  27. char gCR[] = "\r";
  28. char gTAB[] = "\t";
  29. char gSP[] = " ";            /* space */
  30. char gEOL[] = "\0";
  31.  
  32.  
  33.  
  34.  
  35. /*         Simple Harmonic motion simulator application.
  36.     The following are included into the project:
  37.     
  38.         mactraps lib
  39.         math lib
  40.         storage lib
  41.         strings lib
  42.         harmonic sim.c 
  43.         harmonic functs.c
  44.         plot.c
  45.         utils.c
  46.         
  47.     
  48.                     Written by :    Byro
  49.                     Date :            03/05/90
  50.                     Modified :        04/27/90
  51.                     
  52.                     
  53.         Version Log:
  54.                         
  55. */
  56.  
  57.  
  58. main()
  59.  
  60. {
  61.  
  62.     Boolean        doneFlag = FALSE, WNEImplemented;
  63.     EventRecord    myEvent;
  64.     Handle        ClipHandle;
  65.     MenuHandle    AppleMenu;
  66.     Rect        DragRect;
  67.     WindowPtr    PlotWindow,
  68.                 ClipboardWindow,
  69.                 StatusWindow;
  70.  
  71.     ToolBoxInit();
  72.     WindowInit(&PlotWindow, "\pPlot Window");
  73.     WindowInit(&StatusWindow, "\pStatus Window");
  74.     SetUpDragRect(&DragRect);
  75.     MenuBarInit(&AppleMenu);
  76.     CreateClipBoardWindow(&ClipboardWindow);
  77.     ClipHandle = NewHandle(NIL);
  78.     MainLoop(&doneFlag, &myEvent, &WNEImplemented, &DragRect, &AppleMenu,
  79.             &PlotWindow, &ClipboardWindow, &StatusWindow, &ClipHandle);
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. ToolBoxInit()    
  88.     
  89.     /* initialization */
  90.  
  91. {    
  92.     InitGraf(&thePort);
  93.     InitFonts();
  94.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  95.     InitWindows();
  96.     InitMenus();
  97.     TEInit();
  98.     InitDialogs(NIL);
  99.     InitCursor();
  100. }
  101.  
  102.  
  103.  
  104.  
  105. WindowInit(AppWindow, windowTitle)
  106.     WindowPtr    *AppWindow;
  107.     Str255        *windowTitle;
  108.     
  109. {    
  110.     /* init window ptr */
  111.     
  112.     *AppWindow = GetNewWindow(BASE_RES_ID, NIL, MOVE_TO_FRONT);
  113.     
  114.     /* if can't open window let user know */
  115.     
  116.     if (*AppWindow == NIL)
  117.     {
  118.         /* system dialog goes here */
  119.         
  120.         ExitToShell();    /* caio */
  121.     }
  122.     
  123.     if (**windowTitle != NIL)
  124.         SetWTitle(*AppWindow, windowTitle);
  125.     
  126.     SetPort(*AppWindow);        /* make current graf port */
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133. SetUpDragRect(DragRect)
  134.     Rect    *DragRect;
  135.     
  136. {
  137.     *DragRect = screenBits.bounds;
  138.     (*DragRect).left += DRAG_THRESHOLD;
  139.     (*DragRect).right -= DRAG_THRESHOLD;
  140.     (*DragRect).bottom -= DRAG_THRESHOLD;
  141. }
  142.  
  143.  
  144.  
  145.  
  146. MenuBarInit(AppleMenu)
  147.     MenuHandle    *AppleMenu;
  148.     
  149. {
  150.     Handle    myMenuBar;
  151.     
  152.     myMenuBar = GetNewMBar(BASE_RES_ID);
  153.     SetMenuBar(myMenuBar);
  154.     *AppleMenu = GetMHandle(APPLE_MENU_ID);
  155.     AddResMenu(*AppleMenu, 'DRVR');
  156.     DrawMenuBar();
  157. }
  158.  
  159.  
  160.  
  161.  
  162. CreateClipBoardWindow(ClipBoardWindow)
  163.     WindowPtr    *ClipBoardWindow;
  164.     
  165. {
  166.  
  167.     if (( *ClipBoardWindow = GetNewWindow(BASE_RES_ID, NIL, MOVE_TO_FRONT))  == NIL)
  168.     {
  169.         NoteAlert(CLIPBOARD_ALERT, NIL);
  170.         /* ExitToShell(); */
  171.     }
  172.     
  173.     /* set window title */
  174.     
  175.     SetWTitle(*ClipBoardWindow, "\p Clip Board");
  176.     
  177.     MoveWindow(*ClipBoardWindow, WINDOW_HOME_LEFT, WINDOW_HOME_RIGHT, 
  178.                     LEAVE_WHERE_IT_IS);
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. MainLoop(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu, 
  186.         PlotWindow, CBWindow, StatWindow, clipHandle)
  187.     Boolean        *doneFlag;
  188.     EventRecord    *myEvent;
  189.     Boolean        *WNEImplemented;
  190.     Rect        *DragRect;
  191.     MenuHandle    *AppleMenu;
  192.     WindowPtr    *PlotWindow,
  193.                 *CBWindow,
  194.                 *StatWindow;
  195.     Handle        *clipHandle;
  196.     
  197. {
  198.     
  199.     /* check to see if multifinder running */
  200.     
  201.     *WNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) != 
  202.                         NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
  203.                         
  204.     /* main program/Event loop */
  205.     
  206.     do
  207.     {
  208.         HandleEvent(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu,
  209.                     PlotWindow, CBWindow, StatWindow, clipHandle);
  210.     }    
  211.     while (*doneFlag == FALSE);
  212. }
  213.  
  214.  
  215.  
  216.  
  217. HandleEvent(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu, 
  218.             PlotWindow, CbWindow, StWindow, clipHandl)
  219.     Boolean        *doneFlag;
  220.     EventRecord    *myEvent;
  221.     Boolean        *WNEImplemented;
  222.     Rect        *DragRect;
  223.     MenuHandle    *AppleMenu;
  224.     WindowPtr    *PlotWindow,
  225.                 *CbWindow,
  226.                 *StWindow;
  227.     Handle        *clipHandl;
  228.     
  229. {
  230.     char         theChar;
  231.     int            Err = noErr;
  232.     MenuHandle    itemHndl;
  233.     
  234.     static float    *xptr, *yptr;        /* ptrs used to alloc/dealloc space */
  235.     static Boolean    plotFile = FALSE;
  236.     
  237.     CursHandle    theCursor;    /* changes cursor to a watch during update */
  238.             
  239.     /* multifinder check */
  240.     
  241.     if (*WNEImplemented)
  242.         WaitNextEvent(everyEvent, myEvent, SLEEP, NIL_MOUSE_REGION);
  243.     else
  244.     {
  245.         SystemTask();    /* time slice for OS */
  246.         GetNextEvent(everyEvent, myEvent);
  247.     }
  248.         
  249.     switch ((*myEvent).what)
  250.     {
  251.         case    mouseDown:
  252.             HandleMouseDown(doneFlag, myEvent,WNEImplemented, DragRect, AppleMenu, 
  253.                             &plotFile, PlotWindow, CbWindow, StWindow);
  254.             break;
  255.  
  256.         case    nullEvent:
  257.         case    mouseUp:
  258.         case    keyUp:
  259.         case    updateEvt:
  260.             /* 
  261.                 see if update is for this window or open file menu selected 
  262.                 & redraw window if either condition true 
  263.             */
  264.             
  265.             if (((WindowPtr) (*myEvent).message == *PlotWindow) || (plotFile == TRUE))
  266.             {
  267.                 theCursor = GetCursor(WATCH_ICON);    /* draw watch while waiting */
  268.                 SetCursor(*theCursor);
  269.                                 
  270.                 BeginUpdate(*PlotWindow);
  271.  
  272.                 /* main plotting routine */
  273.                 
  274.                 SetPort(*PlotWindow);
  275.                 Err = MainPlot(PlotWindow, &plotFile, &xptr, &yptr);
  276.                 EndUpdate(*PlotWindow);
  277.                 
  278.                 InitCursor();        /* restore cursor to default icon */
  279.                 plotFile = FALSE;    /* reset after use */
  280.                 if (Err == CANCEL_BUTTON)
  281.                 {
  282.                     /* reenable menu selection if cancel button hit */
  283.                     
  284.                     itemHndl = GetMHandle(OPTION_MENU_ID);
  285.                     EnableItem(itemHndl, PLOT_ITEM);
  286.                     break;    /* get out of event loop now! */
  287.                 }
  288.             }
  289.             
  290.             /* chk if update for clip board window */
  291.             
  292.             if ((WindowPtr) (*myEvent).message == *CbWindow)
  293.             {
  294.                 BeginUpdate(*CbWindow);
  295.                 SetPort(*CbWindow);
  296.                 DisplayClipboard(CbWindow, clipHandl);
  297.                 ShowWindow(*CbWindow);
  298.                 EndUpdate(*CbWindow);
  299.             }
  300.             
  301.             /* chk if update for status window */
  302.             
  303.             if ((WindowPtr) (*myEvent).message == *StWindow)
  304.             {
  305.                 BeginUpdate(*StWindow);
  306.                 SetPort(*StWindow);
  307.                 if ((doStatus((double) NIL, (double) NIL, 
  308.                   (double) NIL, "\p", StWindow)) == TRUE)
  309.                 {
  310.                     ShowWindow(*StWindow);
  311.                 }
  312.                 EndUpdate(*StWindow);
  313.             }
  314.             break;
  315.         case    keyDown:
  316.         case    autoKey:
  317.             theChar = (*myEvent).message & charCodeMask;
  318.             if (((*myEvent).modifiers & cmdKey) != 0)
  319.                 HandleMenuChoice(MenuKey(theChar), doneFlag, AppleMenu, &plotFile,
  320.                                 CbWindow, StWindow, PlotWindow);
  321.             break;
  322.         case    activateEvt:
  323.             break;
  324.         default:
  325.             break;
  326.     }
  327.     
  328.     if (*doneFlag == TRUE)
  329.     {
  330.         if (xptr != NIL)        /* release space if no errors */
  331.         {
  332.             if ((free(xptr)) == MinusOne)
  333.                 HandleError(FREE_MEM_ALERT); /* error dialog */
  334.         }
  335.             
  336.         if (yptr != NIL)
  337.         {
  338.             if ((free(yptr)) == MinusOne)
  339.                 HandleError(FREE_MEM_ALERT); /* error dialog */
  340.         }
  341.     }    
  342. }
  343.  
  344.  
  345.  
  346.  
  347. HandleMouseDown(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu, 
  348.                 plotFilePtr, plotWindow, cBwindow, sWindow)
  349.     Boolean        *doneFlag;
  350.     EventRecord    *myEvent;
  351.     Boolean        *WNEImplemented;
  352.     Rect        *DragRect;
  353.     MenuHandle    *AppleMenu;
  354.     Boolean        *plotFilePtr;
  355.     WindowPtr    *plotWindow,
  356.                 *cBwindow,
  357.                 *sWindow;
  358.     
  359. {
  360.     Boolean            StillInGoAway;
  361.     WindowPtr        whichWindow;
  362.     short int        thePart;
  363.     long int        menuChoice;
  364.     MenuHandle        disableHandle;
  365.     static WindowPtr    oldPort;
  366.     
  367.     thePart = FindWindow((*myEvent).where, &whichWindow);
  368.     switch(thePart)
  369.     {
  370.         case    inContent:
  371.                             /* activate the window that was pt'd @  */
  372.                             
  373.                             GetPort(&oldPort);    /* save current graph port  */
  374.                             SetPort(whichWindow);
  375.                             SelectWindow(whichWindow);
  376.                             break;
  377.         case    inMenuBar:
  378.                             menuChoice = MenuSelect((*myEvent).where);
  379.                             HandleMenuChoice(menuChoice, doneFlag, AppleMenu, plotFilePtr, 
  380.                                                 cBwindow, sWindow, plotWindow);
  381.                             break;
  382.         case    inSysWindow:
  383.                             SystemClick(myEvent, whichWindow);
  384.                             break;
  385.         case    inDrag:
  386.                             DragWindow(whichWindow, (*myEvent).where, DragRect);
  387.                             break;
  388.         case    inGoAway:
  389.                             /* highlight close box when clicked */
  390.                             
  391.                             StillInGoAway = TrackGoAway(whichWindow, (*myEvent).where);
  392.                             if (StillInGoAway)
  393.                             {
  394.                                 /* re-enable menu selection when window closed */
  395.                                 
  396.                                 if (whichWindow == *plotWindow)
  397.                                 {
  398.                                     disableHandle = GetMHandle(OPTION_MENU_ID);
  399.                                     EnableItem(disableHandle, PLOT_ITEM);
  400.                                 }
  401.                                 
  402.                                 if (whichWindow == *cBwindow)
  403.                                 {
  404.                                     disableHandle = GetMHandle(EDIT_MENU_ID);
  405.                                     EnableItem(disableHandle, CLIPBOARD_ITEM);
  406.                                 }
  407.                                 
  408.                                 if (whichWindow == *sWindow)
  409.                                 {
  410.                                     disableHandle = GetMHandle(OPTION_MENU_ID);
  411.                                     EnableItem(disableHandle, STATUS_ITEM);
  412.                                 }
  413.                                 
  414.                                 /* restore old graph port */
  415.                                 
  416.                                 SetPort(oldPort);
  417.                                 HideWindow(whichWindow);
  418.                                 SendBehind(whichWindow, NIL);
  419.                             }
  420.                             break;
  421.         default:
  422.                             break;
  423.     }
  424. }
  425.  
  426.  
  427.  
  428.  
  429. HandleMenuChoice(menuChoice, doneFlag, AppleMenu, plotFptr, cbWindow, 
  430.                     stWindow, pltWindow)
  431.     long        menuChoice;
  432.     Boolean        *doneFlag;
  433.     MenuHandle    *AppleMenu;
  434.     Boolean        *plotFptr;
  435.     WindowPtr    *cbWindow,
  436.                 *stWindow,
  437.                 *pltWindow;
  438.  
  439. {
  440.     int theMenu;
  441.     int theItem;
  442.     
  443.     if (menuChoice != FALSE)
  444.     {
  445.         theMenu = HiWord(menuChoice);
  446.         theItem = LoWord(menuChoice);
  447.         switch (theMenu)
  448.         {
  449.             case APPLE_MENU_ID:
  450.                                 HandleAppleChoice(theItem, AppleMenu);
  451.                                 break;
  452.             case EDIT_MENU_ID:
  453.                                 HandleEditChoice(theItem, cbWindow, pltWindow);
  454.                                 break;
  455.             case FILE_MENU_ID:
  456.                                 HandleFileChoice(theItem, doneFlag);
  457.                                 break;
  458.             case OPTION_MENU_ID:
  459.                                 HandleOptionChoice(theItem, plotFptr, stWindow);
  460.                                 break;
  461.             default:
  462.                                 break;
  463.         }
  464.         HiliteMenu(0);
  465.     }
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472. HandleAppleChoice(theItem, AppleMenu)
  473.     int theItem;
  474.     MenuHandle    *AppleMenu;
  475.  
  476. {
  477.     Str255         accName;
  478.     int            accNumber;
  479.     short int    itemNumber;
  480.     DialogPtr    AboutDialog;
  481.     
  482.     switch (theItem)
  483.     {
  484.         case ABOUT_ITEM:
  485.                             NoteAlert(ABOUT_ALERT, NIL);
  486.                             break;
  487.         default:    
  488.                             GetItem(*AppleMenu, theItem, accName);
  489.                             accNumber = OpenDeskAcc(accName);
  490.                             break;
  491.     }
  492. }
  493.  
  494.  
  495.  
  496.  
  497. HandleEditChoice(theItem, theCbWindow, thePlotWindow)
  498.     int            theItem;
  499.     WindowPtr    *theCbWindow,
  500.                 *thePlotWindow;
  501.     
  502. {
  503.     long        Err, *myPtr, ptr = NIL;
  504.     MenuHandle    editHandle;
  505.     PScrapStuff    myScrapPtr;
  506.     Str255        myEmptyStr;
  507.         
  508.     myPtr = (long *) gSP;
  509.  
  510.     /* generate update evnt for clipboard window */
  511.     
  512.     switch (theItem)
  513.     {
  514.         case COPY_ITEM:
  515.                         /* defined by application */
  516.                         break;
  517.         case PASTE_ITEM:
  518.                         /* defined by application */
  519.                         break;
  520.         case CLEAR_ITEM:
  521.                         /* clear clipboard; refer to I.M. I:385-386, I:461 */
  522.                         
  523.                         myScrapPtr = InfoScrap();
  524.                         Err = ZeroScrap();
  525.                         Err = PutScrap(1, 'PAT ', myPtr);
  526.                         SetPort(*theCbWindow);                                
  527.                         EraseRect(&(*theCbWindow)->portRect);
  528.                         UnloadScrap();    /* put it on disk clipboard */
  529.                         break;
  530.         case CLIPBOARD_ITEM:
  531.                         /* disable item when selected */
  532.                             
  533.                         editHandle = GetMHandle(EDIT_MENU_ID);
  534.                         DisableItem(editHandle, CLIPBOARD_ITEM);
  535.                             
  536.                         SetPort(*theCbWindow);
  537.                         SelectWindow(*theCbWindow);
  538.                         ShowWindow(*theCbWindow);
  539.                         break;
  540.         default:
  541.                         break;
  542.     }
  543. }
  544.  
  545.  
  546.  
  547.  
  548. HandleFileChoice(theItem, doneFlag)
  549.     int theItem;
  550.     Boolean    *doneFlag;
  551.  
  552. {
  553.     OSErr         DoLaunch();        /* forward declaration */
  554.     Boolean     SubLaunch;        /* sublaunch if true and launch if false */
  555.     int            Err = noErr;
  556.     MenuHandle    fileHandle;
  557.     
  558.     switch (theItem)
  559.     {
  560.         case TRANSFER_ITEM:
  561.                         /* quits current routine after launch */
  562.                         
  563.                         SubLaunch = FALSE;    
  564.                         Err = DoLaunch(SubLaunch); 
  565.                         break;
  566.         case QUIT_ITEM:
  567.                         *doneFlag = TRUE;
  568.                         break;
  569.     }
  570.     
  571.     /* handle launch failure */
  572.     
  573.     if (Err != noErr)
  574.         NoteAlert(LAUNCH_ALERT, NIL);
  575. }
  576.  
  577.  
  578.  
  579.  
  580. HandleOptionChoice(Item, PlotFPtr, sWindow)
  581.     int            Item;
  582.     Boolean        *PlotFPtr;
  583.     WindowPtr    *sWindow;
  584.     
  585. {
  586.     CursHandle            theCursor;
  587.     double                atof(),
  588.                         Coeff = (double) NIL,
  589.                         Mass = (double) NIL,
  590.                         Elast = (double) NIL;
  591.     MenuHandle            OptionHndl;
  592.     SavedSettings        getSettings,
  593.                         Settings;
  594.     SavedSettingsPtr    SettingsPtr;
  595.     
  596.     SettingsPtr = &getSettings;
  597.     
  598.     switch (Item)
  599.     {
  600.         case SIMULATE_ITEM:
  601.                             /* get input from dialog.  Convert strings to floats and 
  602.                                 pass into calc harmonics if the structure ret'd from 
  603.                                 handle dialog is valid */
  604.                                 
  605.                             if (HandleDialog(&SettingsPtr) == TRUE)
  606.                             {
  607.                                 theCursor = GetCursor(WATCH_ICON);    /* wait cursor */
  608.                                 SetCursor(*theCursor);
  609.  
  610.                                 PtoC(&getSettings, &Settings);
  611.                                 Coeff = atof(Settings.amp);
  612.                                 Mass = atof(Settings.mass);
  613.                                 Elast = atof(Settings.elastic);
  614.                                 CalcHarmonic(Coeff, atof(Settings.frict), Elast, Mass,
  615.                                   (int) atof(Settings.start), (int) atof(Settings.fini), 
  616.                                   (int) atof(Settings.ink), Settings.xVal, Settings.yVal);
  617.                                 doStatus(Coeff, Mass, Elast, Settings.simName, sWindow);
  618.                             }
  619.                             InitCursor();    /* reset to default cursor */
  620.                             break;
  621.         case PLOT_ITEM:
  622.                             OptionHndl = GetMHandle(OPTION_MENU_ID);
  623.                             DisableItem(OptionHndl, PLOT_ITEM);
  624.                             *PlotFPtr = TRUE;
  625.                             break;
  626.         case STATUS_ITEM:
  627.                             /* disable menu selection */
  628.                             
  629.                             OptionHndl = GetMHandle(OPTION_MENU_ID);
  630.                             DisableItem(OptionHndl, STATUS_ITEM);
  631.                             
  632.                             /* display status window */
  633.                             
  634.                             SetPort(*sWindow);
  635.                             if ((doStatus(Coeff, Mass, Elast, "\p", sWindow)) == TRUE)
  636.                             {
  637.                                 SelectWindow(*sWindow);
  638.                                 ShowWindow(*sWindow);
  639.                             }
  640.                             else
  641.                             {
  642.                                 OptionHndl = GetMHandle(OPTION_MENU_ID);
  643.                                 EnableItem(OptionHndl, STATUS_ITEM);
  644.                             }
  645.                             break;
  646.         default:
  647.                             break;
  648.     }
  649. }
  650.  
  651.  
  652.  
  653.  
  654. DisplayClipboard(BoardWindow, ClipHandle)
  655.     WindowPtr    *BoardWindow;
  656.     Handle        *ClipHandle;
  657.     
  658. /* comment later */
  659.  
  660. {
  661.     Rect    myRect;
  662.     long    length,
  663.             offset;
  664.             
  665.     if ((length = GetScrap(*ClipHandle, 'TEXT', &offset)) < 0)
  666.     {
  667.         if ((length = GetScrap(*ClipHandle, 'PICT', &offset)) < 0)
  668.             ;/* NoteAlert(CLIPBOARD_ALERT, NIL); */
  669.         else
  670.         {
  671.             /* display pict data */
  672.             
  673.             myRect = (*BoardWindow)->portRect;
  674.             CenterPict(*ClipHandle, &myRect);
  675.             DrawPicture(*ClipHandle, &myRect);
  676.         }
  677.     }
  678.     else
  679.     {
  680.         /* display text data */
  681.         
  682.         HLock(*ClipHandle);
  683.         TextBox(**ClipHandle, length, &(thePort->portRect), teJustLeft);
  684.         HUnlock(*ClipHandle);
  685.     }
  686. }
  687.  
  688.  
  689.  
  690.  
  691.  
  692. OSErr LaunchIt(pLnch)
  693.     pLaunchStruct pLnch;
  694.  
  695. /*
  696.     This function is pascal call back routine.  It lanches nu apps; refer
  697.     to pp. 118 & 119 in the ls c manual for more info.
  698.         note:    result code < 0 means error
  699. */
  700.  
  701. {
  702.     asm
  703.     {
  704.         move.l    8(sp),a0    /* pops ptr into a0 */
  705.         _Launch                /* calls launch */
  706.                             /* OSErr ret'd in d0 */
  707.     }
  708. }
  709.  
  710.  
  711.  
  712.  
  713. OSErr DoLaunch(subLaunch)
  714.     Boolean    subLaunch;        /* sublaunch if true & launch if false */
  715.     
  716. {
  717.     LaunchStruct    myLaunch;
  718.     Point            where;                /* where to display dialog */
  719.     SFReply            reply;                /* reply record */
  720.     SFTypeList        myFileTypes;        /* we only want appls */
  721.     short int        numFileTypes = 1;
  722.     HFileInfo        myPB;
  723.     StringPtr        dirNameStr;
  724.     OSErr            err;
  725.     OSErr            LaunchIt();            /* forward declare funct */
  726.     
  727.     where.h = 80;
  728.     where.v = 90;
  729.     myFileTypes[0] = 'APPL';            /* only appls */
  730.     
  731.     /* let the user choose the file to launch */
  732.     
  733.     SFGetFile(where, "", NIL, numFileTypes, myFileTypes, NIL, &reply);
  734.     
  735.     if (reply.good)
  736.     {
  737.         dirNameStr = (StringPtr) &reply.fName;    /* init to file selected */
  738.         
  739.         /* get finder flags */
  740.         
  741.         myPB.ioCompletion = NIL;
  742.         myPB.ioNamePtr = dirNameStr;
  743.         myPB.ioVRefNum = reply.vRefNum;
  744.         myPB.ioFDirIndex = 0;
  745.         myPB.ioDirID = 0;
  746.         err = PBGetCatInfo((CInfoPBPtr) &myPB, FALSE);
  747.         if (err != noErr)
  748.             return(err);    /* how to handle this? */
  749.         
  750.         /* set the current volume to where the target appl is */
  751.         
  752.         err = SetVol(NIL, reply.vRefNum);
  753.         if (err != noErr)
  754.             return(err);    /* ditto */
  755.             
  756.         /* set up the launch params */
  757.         
  758.         myLaunch.pfName = (StringPtr) &reply.fName;        /* ptr to our filename */
  759.         myLaunch.param = 0;        /* we don't want alt screen or sound buffs */
  760.             
  761.         /* set up LC so as to tell launch that htere is non-junk next */
  762.         
  763.         myLaunch.LC[0] = 'L';    myLaunch.LC[1] = 'C';
  764.         myLaunch.extBlockLen = 6;    /* len of param blk past this long word */
  765.         
  766.         /* copy flags; set bit 6 of low byte to 1 for RO access: */
  767.         
  768.         myLaunch.fFlags = myPB.ioFlFndrInfo.fdFlags;    /* from _GetCatInfo */
  769.         
  770.         /* test sublaunch & set launch flags accordingly */
  771.         
  772.         if (subLaunch)
  773.             myLaunch.launchFlags = 0xC0000000;    /* set 2 high bit for sublaunch */
  774.         else
  775.             myLaunch.launchFlags = 0x00000000;    /* launch & quit */
  776.         /* Debugger(); */
  777.         err = LaunchIt(&myLaunch);        /* call _Launch */
  778.         if (err < 0)
  779.         {
  780.         /* launch failed, put up alert to tell user */
  781.         /* LaunchFailed();    */
  782.             return(err);
  783.         }
  784.         else
  785.             return(noErr);
  786.     }
  787. }
  788.  
  789.  
  790.  
  791.  
  792. CenterPict(thePicture, myRectPtr)
  793.     PicHandle    thePicture;
  794.     Rect        *myRectPtr;
  795.     
  796. {
  797.     Rect    windRect, 
  798.             pictureRect;
  799.             
  800.     windRect = *myRectPtr;
  801.     pictureRect = (**(thePicture)).picFrame;
  802.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom
  803.                         - pictureRect.top)) / 2 + windRect.top;
  804.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  805.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right 
  806.                         - pictureRect.left)) / 2 + windRect.left;
  807.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  808. }
  809.  
  810.